#include "GnashUtils.h"
#include "managers/GnashManager.h"

using namespace gnash;


as_value GnashUtils::onLoadCallBack(const fn_call& f)
{
//	as_object* obj = ensure<ValidThis>(f);

    if (!f.nargs)
        return as_value();

//	cout << "onLoadCallBack" << f.dump_args() << endl;

    MovieClip* mc = f.arg(0).toMovieClip();
    if(!mc || mc==0) return as_value();


    DisplayObject* dObj = f.arg(0).toDisplayObject();
    if(!dObj) return as_value();

//    as_object* o = f.arg(0).to_object(getGlobal(f));

//    lua_State* L = OgreApplication::getSingletonPtr()->L;

//    object g = globals(L);

    //g["this"] = dObj->getTarget();
//    g["this"] = dObj;
//
//    std::string str = f.arg(1).to_string(8);
//    luaL_dostring(L, str.c_str());
//
//    g["this"] = nil;

	return as_value();
}


void GnashUtils::clearTexture(Ogre::Texture* tex)
{
    unsigned int idx;
    int x, y;

    // OGRE TEXTURE LOCK
    // get the texture pixel buffer
    int texw=tex->getWidth();
    int texh=tex->getHeight();
    Ogre::HardwarePixelBufferSharedPtr pixelBuffer = tex->getBuffer();

    // lock the pixel buffer and get a pixel box
    pixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
    const Ogre::PixelBox& pixelBox = pixelBuffer->getCurrentLock();

    Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);

    // FILL!
    for (x=0, y=0; y<texh; ){
        idx=(x*4)+y*pixelBox.rowPitch*4;

        // paint
        pDest[idx]=255;//b
        pDest[idx+1]=0;//g
        pDest[idx+2]=255;//r
        pDest[idx+3]=255;//a

        x++;
        if (x>=texw)
        {
            x=0;
            y++;
        }
    }

    // UNLOCK EVERYTHING!
    // unlock the pixel buffer
    pixelBuffer->unlock();
}

void GnashUtils::blit(unsigned char* bmp, Ogre::Texture* tex)
{
tex->getBuffer()->blitFromMemory(PixelBox(tex->getWidth(), tex->getHeight(), tex->getDepth(), PF_A8R8G8B8, bmp));
/*
HardwarePixelBufferSharedPtr pixelBuffer = tex->getBuffer();
pixelBuffer->lock(HardwareBuffer::HBL_DISCARD);
const PixelBox& pixelBox = pixelBuffer->getCurrentLock();
uint8* destinationBuffer = static_cast<uint8*>(pixelBox.data);
int sizeBytes = tex->getWidth() * tex->getHeight() * tex->getDepth() * 4;
memcpy(destinationBuffer, bmp, sizeBytes);
pixelBuffer->unlock();
*/
}


gnash::key::code GnashUtils::getGnashKey(OIS::KeyCode e)
{
  switch(e)
  {
  case OIS::KC_0: return gnash::key::_0;
  case OIS::KC_1: return gnash::key::_1;
  case OIS::KC_2: return gnash::key::_2;
  case OIS::KC_3: return gnash::key::_3;
  case OIS::KC_4: return gnash::key::_4;
  case OIS::KC_5: return gnash::key::_5;
  case OIS::KC_6: return gnash::key::_6;
  case OIS::KC_7: return gnash::key::_7;
  case OIS::KC_8: return gnash::key::_8;
  case OIS::KC_9: return gnash::key::_9;

  case OIS::KC_A: return gnash::key::a;
  case OIS::KC_B: return gnash::key::b;
  case OIS::KC_C: return gnash::key::c;
  case OIS::KC_D: return gnash::key::d;
  case OIS::KC_E: return gnash::key::e;
  case OIS::KC_F: return gnash::key::f;
  case OIS::KC_G: return gnash::key::g;
  case OIS::KC_H: return gnash::key::h;
  case OIS::KC_I: return gnash::key::i;
  case OIS::KC_J: return gnash::key::j;
  case OIS::KC_K: return gnash::key::k;
  case OIS::KC_L: return gnash::key::l;
  case OIS::KC_M: return gnash::key::m;
  case OIS::KC_N: return gnash::key::n;
  case OIS::KC_O: return gnash::key::o;
  case OIS::KC_P: return gnash::key::p;
  case OIS::KC_Q: return gnash::key::q;
  case OIS::KC_R: return gnash::key::r;
  case OIS::KC_S: return gnash::key::s;
  case OIS::KC_T: return gnash::key::t;
  case OIS::KC_U: return gnash::key::u;
  case OIS::KC_V: return gnash::key::v;
  case OIS::KC_W: return gnash::key::w;
  case OIS::KC_X: return gnash::key::x;
  case OIS::KC_Y: return gnash::key::y;
  case OIS::KC_Z: return gnash::key::z;


  case OIS::KC_SPACE: return gnash::key::SPACE;
  case OIS::KC_BACK: return gnash::key::BACKSPACE;
  case OIS::KC_RETURN: return gnash::key::ENTER;
  case OIS::KC_ESCAPE: return gnash::key::ESCAPE;
  case OIS::KC_TAB: return gnash::key::TAB;
  case OIS::KC_GRAVE: return gnash::key::EGRAVE;

  case OIS::KC_PERIOD: return gnash::key::PERIOD;
  case OIS::KC_COMMA: return gnash::key::COMMA;
  case OIS::KC_COLON: return gnash::key::COLON;


  case OIS::KC_F1: return gnash::key::F1;
  case OIS::KC_F2: return gnash::key::F2;
  case OIS::KC_F3: return gnash::key::F3;
  case OIS::KC_F4: return gnash::key::F4;
  case OIS::KC_F5: return gnash::key::F5;
  case OIS::KC_F6: return gnash::key::F6;
  case OIS::KC_F7: return gnash::key::F7;
  case OIS::KC_F8: return gnash::key::F8;
  case OIS::KC_F9: return gnash::key::F9;
  case OIS::KC_F10: return gnash::key::F10;
  case OIS::KC_F11: return gnash::key::F11;
  case OIS::KC_F12: return gnash::key::F12;
  case OIS::KC_F13: return gnash::key::F13;
  case OIS::KC_F14: return gnash::key::F14;
  case OIS::KC_F15: return gnash::key::F15;

  default:
    //throw "Bad Key";
    return gnash::key::INVALID;
  }
}

